﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using dbo;
namespace MiracleIsland
{
    class Player:IDisposable
    {
        private int Id;
        public Socket cs;
        private Queue<string> msg;
        public Player(Socket _cs) 
        {
            cs = _cs;
            byte[] buffer = new byte[1024 * 16];
            cs.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, OnRecieveMsg, buffer);
        }
        void OnRecieveMsg(IAsyncResult ir)
        {
            int length = cs.EndReceive(ir);
            byte[] OrigBuffer = (byte[])ir.AsyncState;
            byte[] NewBuffer = new byte[length];
            for (int i = 0; i < length; i++)
            {
                NewBuffer[i] = OrigBuffer[i];
            }
            msg.Enqueue(Encoding.UTF8.GetString(NewBuffer));
            cs.BeginReceive(OrigBuffer, 0, OrigBuffer.Length, SocketFlags.None, OnRecieveMsg, OrigBuffer);
        }
        public void Dispose() 
        {
            cs.Close();
            GC.Collect(0,GCCollectionMode.Forced);
        }
    }
}
